<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/2/25
 * Time: 17:08
 */

namespace app\api\controller;


use think\Db;

class Beautytips extends Base
{
    public $user_id;
    public $create_time;
    public $page;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status' => 301, 'msg' => $this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 美丽技巧列表
     * @return \think\response\Json
     * list_type     1 推荐列表    2 收藏列表
     * tips_cate     分类ID
     * page          页码
     */
    public function beautytipsList(){
        $list_type = input('list_type') ? input('list_type') : 0; // 1 推荐列表   2 收藏列表
        $tips_cate = input('tips_cate') ? input('tips_cate') : 0; // 分类ID
        if($list_type == 1){
            $where['bt_status'] = 1;
            $where['bt_is_recommend'] = 1;
//            $unlike_cate = Db::table('up_beauty_tips_cate_notlike')
//                ->where('btcn_user_id', $this->user_id)
//                ->column('btcn_btc_id');
//            if($unlike_cate){
//                $where['bt_cate'] = ['NOTIN', $unlike_cate];
//            }
        }elseif($list_type == 2){
            $myCollect = Db::table('up_beauty_tips_collect')
                ->where('btc_user_id', $this->user_id)
                ->column('btc_bt_id');
            $where['bt_id'] = ['IN', $myCollect];
        }else{
            $where['bt_status'] = 1;
            if($tips_cate != 0){
                $where['bt_cate'] = $tips_cate;
            }
        }
        $list = Db::table('up_beauty_tips')
            ->where($where)
            ->field('bt_id, bt_name, bt_image, bt_play_number, bt_duration, bt_cate')
            ->order('bt_time desc')
            ->page($this->page, 10)
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 美丽技巧反馈类型
     * @return \think\response\Json
     */
    public function feedbackType(){
        $type = Db::table('up_beauty_tips_feedback_type')
            ->where('btft_is_del', 1)
            ->field('btft_id, btft_name')
            ->select();
        if($type){
            return json(['status' => 200, 'msg' => $type]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 提交美丽技巧反馈
     * @return \think\response\Json
     * bt_id       美丽技巧ID
     * btft_id     反馈类型ID
     */
    public function beautytipsFeedback(){
        $bt_id = input('bt_id');
        $btft_id = input('btft_id');
        $res = Db::table('up_beauty_tips_feedback')
            ->insert([
                'btf_type_id' => $btft_id,
                'btf_bt_id' => $bt_id,
                'btf_user_id' => $this->user_id,
                'btf_time' => $this->create_time
            ]);
        if($res){
            return json(['status' => 200, 'msg' => '反馈成功']);
        }else{
            return json(['status' => 0, 'msg' => '反馈失败']);
        }
    }
    
    /**
     * 美丽技巧收藏/取消收藏
     * @return \think\response\Json
     * bt_id     美丽技巧ID
     */
    public function beautytipsCollect(){
        $bt_id = input('bt_id');
        $exist = Db::table('up_beauty_tips_collect')
            ->where('btc_bt_id', $bt_id)
            ->where('btc_user_id', $this->user_id)
            ->field('btc_id')
            ->find();
        if($exist){ // 取消收藏
            $res = Db::table('up_beauty_tips_collect')->delete($exist['btc_id']);
            $collect = 2;
        }else{
            $res = Db::table('up_beauty_tips_collect')
                ->insert([
                    'btc_bt_id' => $bt_id,
                    'btc_user_id' => $this->user_id,
                    'btc_time' => date('Y-m-d H:i:s')
                ]);
            $collect = 1;
        }
        if($res){
            return json(['status' => 200, 'msg' => $collect]);
        }else{
            return json(['status' => 0, 'msg' => false]);
        }
    }
    
    /**
     * 不感兴趣
     * @return \think\response\Json
     * btc_id    美丽技巧分类ID
     */
    public function beautytipsNotlike(){
        $btc_id = input('btc_id');
        $exist = Db::table('up_beauty_tips_cate_notlike')
            ->where('btcn_btc_id', $btc_id)
            ->where('btcn_user_id', $this->user_id)
            ->find();
        if($exist){
            return json(['status' => 200, 'msg' => true]);
        }else{
            $res = Db::table('up_beauty_tips_cate_notlike')
                ->insert([
                    'btcn_btc_id' => $btc_id,
                    'btcn_user_id' => $this->user_id,
                    'btcn_time' => $this->create_time
                ]);
            if($res){
                return json(['status' => 200, 'msg' => true]);
            }else{
                return json(['status' => 0, 'msg' => false]);
            }
        }
    }
    
    /**
     * 美丽技巧分类
     * @return \think\response\Json
     */
    public function beautytipsCate(){
        $cate = Db::table('up_beauty_tips_cate')
            ->where('btc_is_del', 1)
            ->field('btc_id, btc_name')
            ->select();
        if($cate){
            return json(['status' => 200, 'msg' => $cate]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 视频详情
     * @return \think\response\Json
     * bt_id     美丽技巧ID
     */
    public function beautytipsDetail(){
        $bt_id = input('bt_id');
        // 播放次数+1
        Db::table('up_beauty_tips')->where('bt_id', $bt_id)->setInc('bt_play_number');
        $detail = Db::table('up_beauty_tips')
            ->where('bt_id', $bt_id)
            ->field('bt_name, bt_status, bt_duration, bt_play_number, bt_video')
            ->find();
        if($detail['bt_status'] != 1){
            return json(['status' => 0, 'msg' => '视频下架或已删除']);
        }
        // 收藏数量
        $collect_number = Db::table('up_beauty_tips_collect')->where('btc_bt_id', $bt_id)->count();
        $detail['collect_number'] = $collect_number;
        // 是否收藏
        $is_collect = Db::table('up_beauty_tips_collect')
            ->where(['btc_bt_id' => $bt_id, 'btc_user_id' => $this->user_id])
            ->find();
        if($is_collect){
            $detail['is_collect'] = 1; // 已收藏
        }else{
            $detail['is_collect'] = 2; // 未收藏
        }
            
        return json(['status' => 200, 'msg' => $detail]);
    }
}